(function($){
	$.fn.hjTree = function(){

		return $(this).each(function(){
			if( !$(this).parents('.tree').length ){
			var tree = $(this);

			tree.addClass('tree');
			tree.find('span:eq(0)').attr('tabindex','0');
			tree.find('span:gt(0)').attr('tabindex','-1');
			tree.find('li:has(ul)')
					.find('>span').addClass('tree-parent').prepend("<span class='icon'>-</span>");

			//bind the custom events
			tree
				.bind('expandTree',function(event){
					var target = $(event.target) || tree.find('span[tabindex=0]');
					target = target.parent();
					target.removeClass('tree-parent-collapsed');
					target.find('span.icon').text('-');
					target.next().removeClass('tree-group-collapsed').show();
				})
				.bind('collapseTree',function(event){
					var target = $(event.target) || tree.find('span[tabindex=0]');
					target = target.parent();
					target.addClass('tree-parent-collapsed');
					target.find('span.icon').text('+');
					target.next().addClass('tree-group-collapsed').hide();
				})
				.bind('toggleTree',function(event){
					var target = $(event.target) || tree.find('span[tabindex=0]');
					if( target.is('span.tree-parent-collapsed span.icon') ){
						target.trigger('expandTree');
					}
					else{
						target.trigger('collapseTree');
					}
				});

			tree.click(function(event){
					var target = $(event.target);
					if( target.is('span.tree-parent span.icon') ){
						target.trigger('toggleTree');
						return false;
					}
				});
			}//end if length
		});
	};
})(jQuery);